package com.snake.springboot.authority.service.assembler;

import com.snake.springboot.authority.api.model.AuthBuyStrategyModel;
import com.snake.springboot.authority.api.model.AuthOperatorStrategyModel;
import com.snake.springboot.authority.api.model.StrategyExpireModel;
import com.snake.springboot.authority.dao.mybatis.entity.AuthOperatorStrategyDO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snake.common.date.DateUtil;
import org.snake.common.utils.ListUtils;
import org.snake.common.utils.PropertyUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 类 {@code AuthStrategyAssmbler}  <br> .
 * <p>权限策略组装器
 * <p>创建时间：2025/02/07
 * @author yangxt
 */
public class AuthStrategyOperatorAssembler {

    private static Logger log= LoggerFactory.getLogger(AuthStrategyOperatorAssembler.class);


    public static List<AuthOperatorStrategyDO> authDOAss(List<AuthOperatorStrategyModel> models) {
        List<AuthOperatorStrategyDO> dos = ListUtils.copy(models, AuthOperatorStrategyDO.class);
        for (AuthOperatorStrategyDO strategyDO:dos){
            if (StringUtils.isEmpty(strategyDO.getExpireTime())){
                strategyDO.setExpireTime(null);
                strategyDO.setExpireState(StrategyExpireModel.EVERLASTING);
            }else {
                strategyDO.setExpireState(StrategyExpireModel.EXPIRE_STATE);
            }
        }

        return dos;
    }

    public static List<AuthOperatorStrategyDO> authBuyDOAss(List<AuthBuyStrategyModel> models,List<AuthOperatorStrategyDO> exist) {
        List<AuthOperatorStrategyDO> dos = new ArrayList<>(models.size());
        Map<String, AuthOperatorStrategyDO> doMap = exist.stream().collect(Collectors.toMap(AuthOperatorStrategyDO::getStrategyId, Function.identity()));
        Date now = new Date();
        for (AuthBuyStrategyModel model:models){
            try {
                Date newExpireDate = DateUtil.addDay(now, model.getDayCount());
                AuthOperatorStrategyDO strategyDO = doMap.get(model.getStrategyId());
                if (strategyDO != null){
                    if (strategyDO.getExpireState().equals(StrategyExpireModel.EXPIRE_STATE)){
                        Date date = DateUtil.parseDate(strategyDO.getExpireTime(), DateUtil.PATTERN_DATE_TIME);
                        if (DateUtil.biggerThan(date,now)){
                            newExpireDate = DateUtil.addDay(date, model.getDayCount());
                        }
                        strategyDO.setExpireTime(DateUtil.formatDateTime(newExpireDate));
                        dos.add(strategyDO);
                    }
                }else {
                    strategyDO = new AuthOperatorStrategyDO();
                    PropertyUtils.copyProperties(strategyDO,model);
                    strategyDO.setExpireTime(DateUtil.formatDateTime(newExpireDate));
                    strategyDO.setExpireState(StrategyExpireModel.EXPIRE_STATE);
                    dos.add(strategyDO);
                }

            }catch (Exception e){
                log.error("组装购买策略数据异常：{}",e);
            }
        }
        return dos;
    }

    public static List<AuthOperatorStrategyDO> authRetDOAss(List<AuthBuyStrategyModel> models, List<AuthOperatorStrategyDO> exist) {
        List<AuthOperatorStrategyDO> dos = new ArrayList<>(models.size());
        Map<String, AuthOperatorStrategyDO> doMap = exist.stream().collect(Collectors.toMap(AuthOperatorStrategyDO::getStrategyId, Function.identity()));
        Date now = new Date();
        for (AuthBuyStrategyModel model:models){
            try {
                AuthOperatorStrategyDO strategyDO = doMap.get(model.getStrategyId());
                if (strategyDO == null){
                    continue;
                }
                if (strategyDO.getExpireState().equals(StrategyExpireModel.EVERLASTING)){
                    exist.remove(strategyDO);
                    continue;
                }
                String expireTime = strategyDO.getExpireTime();
                Date date = DateUtil.addDay(DateUtil.parseDate(expireTime, DateUtil.PATTERN_DATE_TIME),-model.getDayCount());
                if (!DateUtil.biggerThan(now,date)){
                    strategyDO.setExpireTime(DateUtil.formatDateTime(date));
                    dos.add(strategyDO);
                }
            }catch (Exception e){
                log.error("组装取消策略数据异常：{}",e);
            }
        }
        return dos;
    }
}
